package come.itheima.springbootwebmybatistlias.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import come.itheima.springbootwebmybatistlias.mapper.EmpMapper;
import come.itheima.springbootwebmybatistlias.pojo.Emp;
import come.itheima.springbootwebmybatistlias.pojo.PageBean;
import come.itheima.springbootwebmybatistlias.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    //依赖注入
    @Autowired
    private EmpMapper empMapper;

    // 基本的分页查询
    /*
    @Override
    public PageBean queryEmp(Integer page, Integer pageSize, String name, Short gender,
                             LocalDate begin, LocalDate end) {
        Integer start = (page - 1) * pageSize;

        // 查询
        Long count = empMapper.count();
        List<Emp> emps = empMapper.queryEmp(start, pageSize,name, gender, begin, end);

        return new PageBean(count, emps);
    }*/


    // 根据分页插件PageHelper分页查询 + 动态SQL
    @Override
    public PageBean queryEmp(Integer page,
                             Integer pageSize,
                             String name,
                             Short gender,
                             LocalDate begin,
                             LocalDate end) {
        // 1、设置分页参数,
        PageHelper.startPage(page, pageSize);
        // 2、执行查询
        List<Emp> empList = empMapper.queryEmp(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) empList;
        // 3、封装PageBean对象
        return new PageBean(p.getTotal(), p.getResult());
    }


    //批量删除员工
    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }


    //新增员工
    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.insert(emp);
    }

    // 根据ID查询员工数据
    @Override
    public Emp queryEmpById(Integer id) {
        return empMapper.queryEmpById(id);
    }

    // 修改员工数据
    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.getByUsernameAndPassword(emp);
    }


}
